package cn.bs.allensocket.good;

/**
 * Created by Allen on 2014/11/7.
 */
public class Godown {
    public static final int max_Size = 100;
    public int curnum;
    Godown(){}
    Godown(int curnum){
        this.curnum = curnum;
    }

    public synchronized void produce(int neednum){
        //测试是否需要生产
        while(neednum + curnum > max_Size){
            System.out.println("需要生产的产品数量" + curnum + "超过剩余库存量" + (max_Size - curnum) + ",暂时不用生产");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //满足就生产
        curnum += neednum;
        System.out.println("已经生产了" + neednum + "产品，" + "现仓库存量为" + curnum);
        notifyAll();

    }

    public synchronized void consume(int neednum){
        while (curnum < neednum){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        curnum -= neednum;
        System.out.println("已经消费了" + neednum + "个产品，当前库存为:" + curnum);
        notifyAll();
    }
}
